package com.qys.livesMall.framework.view;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.utils.bean.BeanProUtils;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.List;

@Data
@Accessors(chain = true)
public class QysPage<T>  implements Serializable {

    /**
     * 总数
     */
    private long total = 0;
    /**
     * 每页显示条数，默认 10
     */
    private long size = 10;

    /**
     * 当前页
     */
    private long current = 1;

    /**
     * 下一页 0：没有，1:有
     */
    private int nextPage = 0;

    private List<T> records;


    public QysPage(){

    }

    public QysPage(int pageNum, int pageSize){
        this.current=pageNum;
        this.size=pageSize;
    }


    public QysPage(Page page, Class clazz) {
        this.setSize(page.getSize());
        this.setCurrent(page.getCurrent());
        this.setTotal(page.getTotal());
        List list=BeanProUtils.copyListPropertiesV2(page.getRecords(),clazz);
        this.setRecords(list);
    }

    public static <T> QysPage to(Page page, Class clazz) {
        QysPage<T> qysPage=new QysPage();
        qysPage.setSize(page.getSize());
        qysPage.setCurrent(page.getCurrent());
        qysPage.setTotal(page.getTotal());
        List list=BeanProUtils.copyListPropertiesV2(page.getRecords(),clazz);
        qysPage.setRecords(list);
        return qysPage;
    }

    public static <T> QysPage to(Paging page) {
        QysPage<T> qysPage=new QysPage();
        qysPage.setSize(page.getSize());
        qysPage.setCurrent(page.getCurrent());
        qysPage.setTotal(page.getTotal());
        qysPage.setRecords(page.getRecords());
        return qysPage;
    }

    public static <T> QysPage to(Paging page, Class clazz) {
        QysPage<T> qysPage=new QysPage();
        qysPage.setSize(page.getSize());
        qysPage.setCurrent(page.getCurrent());
        qysPage.setTotal(page.getTotal());
        List list=BeanProUtils.copyListPropertiesV2(page.getRecords(),clazz);
        qysPage.setRecords(list);
        return qysPage;
    }

    public static <T> QysPage build(BaseQuery query){
        QysPage<T> qysPage=new QysPage();
        qysPage.setSize(query.getPageSize());
        qysPage.setCurrent(query.getPageNum());
        return qysPage;
    }

    public QysPage<T> setRecords(List<T> records) {
        this.records = records;
        if (records!=null&&records.size()>=this.size){
            this.nextPage=1;
        }
        return this;
    }

    public QysPage<T> setSize(long size) {
        this.size = size;
        if (records!=null&&records.size()>=this.size){
            this.nextPage=1;
        }
        return this;
    }
}
